﻿using UnityEngine;
using System.Collections;
using System;
using UniLua;

namespace TCG.FFI
{
	public static class UnityRectLib 
	{
		#region stuff for exporting to lua
		public static string LIB_NAME = "UnityRectLib";
		#endregion

		#region variables
		public static int mf_center (ILuaState luaState)
		{
			Rect r = (Rect)LuaTools.L_CheckUData (luaState, -1, LIB_NAME);
			LuaTools.L_PushUserData (luaState, new Vector3 (r.center.x, r.center.y), UnityVector3Lib.LIB_NAME);
			return 1;
		}

		public static int mf_height (ILuaState luaState)
		{
			Rect r = (Rect)LuaTools.L_CheckUData (luaState, -1, LIB_NAME);
			luaState.PushNumber (r.height);
			return 1;
		}

		public static int mf_width (ILuaState luaState)
		{
			Rect r = (Rect)LuaTools.L_CheckUData (luaState, -1, LIB_NAME);
			luaState.PushNumber (r.width);
			return 1;
		}

		public static int mf_get_x (ILuaState luaState)
		{
			Rect r = (Rect)LuaTools.L_CheckUData (luaState, -1, LIB_NAME);
			luaState.PushNumber (r.x);
			return 1;
		}

		public static int mf_get_x_max (ILuaState luaState)
		{
			Rect r = (Rect)LuaTools.L_CheckUData (luaState, -1, LIB_NAME);
			luaState.PushNumber (r.xMax);
			return 1;
		}

		public static int mf_get_y (ILuaState luaState)
		{
			Rect r = (Rect)LuaTools.L_CheckUData (luaState, -1, LIB_NAME);
			luaState.PushNumber (r.y);
			return 1;
		}

		public static int mf_get_y_max (ILuaState luaState)
		{
			Rect r = (Rect)LuaTools.L_CheckUData (luaState, -1, LIB_NAME);
			luaState.PushNumber (r.yMax);
			return 1;
		}
		#endregion

		#region constructors
		public static int lf_NewRect (ILuaState luaState)
		{
			float left = (float)luaState.L_CheckNumber (1);
			float top = (float)luaState.L_CheckNumber (2);
			float width = (float)luaState.L_CheckNumber (3);
			float height = (float)luaState.L_CheckNumber (4);
			LuaTools.L_PushUserData (luaState, new Rect (left, top, width,height), LIB_NAME);
			return 1;
		}
		#endregion

		#region functions
		public static int mf_Contains (ILuaState luaState)
		{
			Rect r = (Rect)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			Vector3 point = (Vector3)LuaTools.L_CheckUData (luaState, 2, UnityVector3Lib.LIB_NAME);
			if (luaState.GetTop () == 2)
			{
				luaState.PushBoolean (r.Contains (point));
			}
			else
			{
				bool allowInverse = luaState.ToBoolean (3);
				luaState.PushBoolean (r.Contains (point, allowInverse));
			}
			return 1;
		}

		public static int mf_Overlaps (ILuaState luaState)
		{
			Rect r = (Rect)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			Rect other = (Rect)LuaTools.L_CheckUData (luaState, 2, LIB_NAME);
			if (luaState.GetTop () == 2)
			{
				luaState.PushBoolean (r.Overlaps (other));
			}
			else
			{
				bool allowInverse = luaState.ToBoolean (3);
				luaState.PushBoolean (r.Overlaps (other, allowInverse));
			}
			return 1;
		}

		public static int mf_Set (ILuaState luaState)
		{
			Rect r = (Rect)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			float left = (float)luaState.L_CheckNumber (2);
			float top = (float)luaState.L_CheckNumber (3);
			float width = (float)luaState.L_CheckNumber (4);
			float height = (float)luaState.L_CheckNumber (5);
			r.Set (left, top, width, height);
			LuaTools.L_PushUserData (luaState, r, LIB_NAME);
			return 1;
		}

		public static int mf___string (ILuaState luaState)
		{
			Rect r = (Rect)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			luaState.PushString (string.Format ("[x={0}, y={1}, w={2}, h={3}]", r.x, r.y, r.width, r.height));
			return 1;
		}
		#endregion

		#region static functions

		#endregion

		#region operators
		public static int mf___eq (ILuaState luaState)
		{
			Rect r = (Rect)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			Rect other = (Rect)LuaTools.L_CheckUData (luaState, 2, LIB_NAME);
			luaState.PushBoolean (r == other);
			return 1;
		}
		#endregion
	}
}
